import axios from 'axios'
import { Toast } from 'mint-ui'

// create an axios instance
const service = axios.create({
//   baseURL: '/api', // url = base url + request url
  timeout: 5000 // request timeout
})


// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    const res = response.data

    // if the custom code is not 20000, it is judged as an error.
    if (!res.success) {
        Toast({
            message: res.message,
            duration: 2000
        });
      return Promise.reject(res.message || 'error')
    } else {
      return res
    }
  },
  error => {
    console.log('err' + error) // for debug
    Toast({
      message: error.message,
      duration: 2000
    })
    return Promise.reject(error)
  }
)

export default service
